//
//  ViewPagerView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/15.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class ViewPagerView: UIView {
    // 图片数量
    private let PIC_COUNT = 5
    // 定时器
    private var timer:Timer? = nil
    // scrollView
    private lazy var scrollView = UIScrollView()
    // 小圆点
    private lazy var pageControl = UIPageControl()
    
    // 回调
    public var complete: ((_ item:Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        init_views()
        init_listener()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ViewPagerView {
    
    private func init_views() -> Void {
        // 入参
        let x = frame.origin.x
        let width = frame.size.width
        let height = frame.size.height
        // scrollView
        self.scrollView.layer.cornerRadius = 5
        self.scrollView.layer.masksToBounds = true
        self.scrollView.frame = CGRect(x: 0, y: x * 0.5, width: width, height: height)
        // pageControl
        self.pageControl.numberOfPages = 5
        self.pageControl.pageIndicatorTintColor = UIColor.orange
        self.pageControl.frame = CGRect(x:width * 0.5, y: height - x * 0.5, width: 0, height: 10)
        // 尺寸
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentSize = CGSize(width: PIC_COUNT * Int(width), height: 0)
        // 分页
        scrollView.isPagingEnabled = true
        // 图片
        for item in 0...4 {
            let img = UIButton()
            img.tag = item
            img.contentMode = .scaleAspectFit
            img.setImage(UIImage(named: "img_0\(item)"), for: .normal)
            img.frame = CGRect(x: CGFloat(item) * width, y: 0, width: width, height: height)
            img.layer.cornerRadius = 5
            img.layer.masksToBounds = true
            scrollView.addSubview(img)
            // 添加点击事件
            img.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
        }
        addSubview(scrollView)
        addSubview(pageControl)
    }
    
    func init_listener() -> Void {
        // 代理
        scrollView.delegate = self
        // 自动滑动
        timer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(nextPic), userInfo: nil, repeats: true)
        // 修改计时器执行优先级别
        RunLoop.current.add(timer!, forMode: .common)
    }
    
    @objc func nextPic() -> Void {
        var curr = pageControl.currentPage
        // 当前是第几页
        if curr == pageControl.numberOfPages - 1{
            curr = 0
        }else {
            curr += 1
        }
        // 获取轮播图片显示第几张
        scrollView.setContentOffset(CGPoint(x: CGFloat(curr) * frame.size.width, y: 0), animated: true)
    }
    
    @objc func clickItem(btn:UIButton) {
        if let callback = complete {
            callback(btn.tag)
        }
    }
}

extension ViewPagerView : UICollectionViewDelegate {
    // 滑动中
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let x = scrollView.contentOffset.x + frame.size.width / 2
        pageControl.currentPage = Int(x / frame.size.width)
    }
    // 开始拖拽
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        // 计时器停止
        timer?.invalidate()
        timer = nil
    }
    // 结束拖拽
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        timer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(nextPic), userInfo: nil, repeats: true)
    }
}

